<template>
    <div class="app-container">
        <el-form ref="queryParamsRef" :inline="true" :model="queryParams" label-width="4rem" label-position="left">
            <el-form-item label="姓名：" prop="name">
                <el-input v-model="queryParams.name" clearable @clear="getList" style="width: 12rem"
                    placeholder="请输入重点对象名称" @keyup.enter.native="getList" />
            </el-form-item>
            <el-form-item label="状态：" prop="state">
                <el-select v-model="queryParams.state" placeholder="选择状态" style="width: 10rem" @change="getList">
                    <el-option value="">全部</el-option>
                    <el-option v-for="item in sys_normal_disable" :key="item.value" :label="item.label"
                        :value="item.value" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="getList">
                    <el-icon class="me-1">
                        <Search />
                    </el-icon>
                    查询
                </el-button>
            </el-form-item>
            <el-form-item>
                <el-button @click="resetQueryParamsHandle(queryParamsRef)">
                    <el-icon class="me-1">
                        <Refresh />
                    </el-icon>
                    重置
                </el-button>
            </el-form-item>
        </el-form>

        <el-divider class="mt-0" />

        <el-row class="mb-3">
            <el-col :span="24">
                <el-space>
                    <el-button type="primary" plain @click="keyPersonManageRef.showAddPersonMange()">
                        <el-icon class="me-2">
                            <Plus />
                        </el-icon>
                        新增
                    </el-button>
                </el-space>
            </el-col>
        </el-row>

        <el-table :data="tableData" v-loading="loading" border
        stripe
            :row-style="tableRowStyle"
        >
            <el-table-column label="照片" width="180">
                <template #default="scope">
                    <template v-if="scope.row.photo">
                        <el-image style="width: 40px; height: 40px" :src="scope.row.photo" lazy />
                    </template>
                </template>
            </el-table-column>
            <el-table-column prop="name" label="姓名" width="180" />
            <el-table-column prop="sex" label="性别" width="180" />
            <el-table-column prop="idnumber" label="身份证" width="180" />
            <el-table-column prop="nation" label="民族" width="180" />
            <el-table-column prop="warninglevel" label="预警级别" width="180" />
            <el-table-column prop="state" label="状态" width="180" />
            <el-table-column prop="address" label="住址" width="180" />
            <el-table-column prop="workplace" label="工作单位" width="180" />
            <el-table-column prop="post" label="职务" width="180" />
            <el-table-column prop="adddate" label="登记时间" width="180" />
            <el-table-column fixed="right" label="操作" width="180">
                <template #default="scope">
                    <el-button type="primary" size="small" link
                        @click="keyPersonManageRef.showEditPersonMange(scope.row)">
                        <el-icon class="me-1">
                            <Edit />
                        </el-icon>
                        编辑
                    </el-button>

                    <el-button type="danger" size="small" link @click="deletePersonHandle(scope.row)">
                        <el-icon class="me-1">
                            <Delete />
                        </el-icon>
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" v-model:page="paging.pageNum" v-model:limit="paging.pageSize" :total="total"
            @pagination="getList" />

        <AddEditKeyPersonManage ref="keyPersonManageRef" @refresh-list="getList" />
    </div>
</template>

<script setup>
import PageHeader from "@/components/PageHeader/index.vue";
import { Delete, Edit, Plus, Refresh, Search } from "@element-plus/icons-vue";
import AddEditKeyPersonManage from "@/views/PlanManage/KeyPersonManage/components/AddEditKeyPersonManage.vue";
import { getCurrentInstance, useTemplateRef } from "vue";
import { deleteKeyPersonManage, getKeyPersonManageList } from "@/api/PlanManage/KeyPersonManage.js";
import { ElMessageBox } from "element-plus";
import PageEnum from "@/enum/PageEnum.js";

const { proxy } = getCurrentInstance();
const {
    sys_normal_disable
} = proxy.useDict('sys_normal_disable')

const queryParamsRef = useTemplateRef('queryParamsRef')
const loading = ref(false)
const total = ref(0)
const paging = ref({
    // 页码
    pageNum: 1,
    // 分页数量
    pageSize: 10
})
const queryParams = ref({
    name: '',
    state: ''
})

const tableData = ref([])

const keyPersonManageRef = useTemplateRef('keyPersonManageRef')

onMounted(() => getList())
// 斑马纹样式函数
const tableRowStyle = ({ row, rowIndex }) => {
    if (rowIndex % 2 === 0) {
        return { backgroundColor: '#e6ecff' }; // 偶数行背景色
    }
    return {}; // 奇数行默认透明
}
// 获取列表
const getList = async () => {
    loading.value = true;
    const res = await getKeyPersonManageList(queryParams.value, paging.value)
    total.value = res.total;
    tableData.value = res.rows;
    loading.value = false
}

// 重置查询内容
const resetQueryParamsHandle = async (elForm) => {
    if (!elForm) return
    elForm.resetFields()
    await getList()
}

// 删除事件
const deletePersonHandle = (data) => {
    ElMessageBox.confirm(
        '确定要删除此重点对象吗？',
        '提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(async () => {
        await deleteKeyPersonManage(data.peopleId).then(async () => {
            await getList()
        })
    }).catch(() => {
    })
}
</script>

<style lang="scss" scoped>
/* 保留斑马纹的同时添加悬停效果 */
:deep().el-table--striped .el-table__body tr.el-table__row--striped:hover td {
  background-color: #a5b8f4; /* 悬停时的红色 */
}

:deep().el-table__body tr:hover td {
  background-color: #a5b8f4; /* 悬停时的红色 */
}
</style>